<?php

namespace app\admin\controller\cms;

use app\admin\controller\AdminController;
use app\admin\service\cms\ArticleService;
use app\admin\validate\cms\ArticleValidate;
use app\admin\validate\file\ExportValidate;
use think\response\Json;

/**
 * 文章管理控制器
 */
class Article extends AdminController
{

    /**
     * 查看文章列表
     * @return Json
     */
    public function lists()
    {
        return $this->data(ArticleService::getList());
    }

    /**
     * 添加文章
     * @return Json
     */
    public function add()
    {
        $params = (new ArticleValidate())->post()->checkParam('add');
        $result =  ArticleService::add($params);
        if(true === $result) {
            return $this->success([], '添加成功');
        } else {
            return $this->error(ArticleService::getError());
        }
    }

    /**
     * 编辑文章
     * @return Json
     */
    public function edit()
    {
        $params = (new ArticleValidate())->post()->checkParam('edit');
        $result = ArticleService::edit($params);
        if(true === $result) {
            return $this->success([], '编辑成功');
        } else {
            return $this->error(ArticleService::getError());
        }
    }

    /**
     * 删除文章
     * @return Json
     */
    public function delete()
    {
        $params = (new ArticleValidate())->delete()->checkParam('delete');
        $result = ArticleService::delete($params);
        if(true === $result) {
            return $this->success([], '删除成功');
        } else {
            return $this->error(ArticleService::getError());
        }
    }

    /**
     * 文章详情
     * @return Json
     */
    public function detail()
    {
        $params = (new ArticleValidate())->checkParam('detail');
        $result = ArticleService::detail($params);
        return $this->data($result);
    }

    /**
     * 更改文章状态
     * @return Json
     */
    public function updateStatus()
    {
        $params = (new ArticleValidate())->post()->checkParam('status');
        $result = ArticleService::updateStatus($params);
        if(true === $result) {
            return $this->success([], '修改成功');
        } else {
            return $this->error(ArticleService::getError());
        }
    }


    /**
     * 导出文章
     * @return Json
     */
    public function export()
    {
        $params = (new ExportValidate())->post()->checkParam('export');
        $result = ArticleService::export($params,$this->adminInfo);
        if(false !== $result) {
            return $this->data($result);
        } else {
            return $this->error(ArticleService::getError());
        }
    }


    /**
     * 导入文章
     * @return Json
     */
    public function import()
    {
        $params = (new ExportValidate())->post()->checkParam('import');
        $result = ArticleService::import($params,$this->adminInfo);
        if(false !== $result) {
            return $this->data($result);
        } else {
            return $this->error(ArticleService::getError());
        }
    }
}